package com.techventus.locations;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class DeleteLocation extends Activity{

	
	String[] locations ;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		this.setContentView(R.layout.deletelocation);
		// ScrollView sv = new ScrollView(this);
	      //  LinearLayout ll = new LinearLayout(this);
	     //   ll.setOrientation(LinearLayout.VERTICAL);
	      //  sv.addView(ll);
		
	        
	    LinearLayout outerlayout= (LinearLayout)  this.findViewById(R.id.deletelocation);
	        
	    SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
	    try{
	    	Log.e("TECHVENTUS","DeleteLocation Attempt to get locationName");
	    	locations = getStringArSQLVertical("SELECT locationName FROM LOCATIONS WHERE locationName != 'Elsewhere';",sql);
	    }catch(Exception y){y.printStackTrace();}
	    sql.close();
	    
	    if(locations!=null){
	    	
	    
	    	
	    	for(int i=0;i<locations.length;i++){
	    		LinearLayout ll = new LinearLayout(this);
	    		ll.setOrientation(LinearLayout.VERTICAL);
	    		TextView loc = new TextView(this);
	    		loc.setText(locations[i]);
	    		loc.setTextSize(23);
	    		
	    		TextView spacer = new TextView(this);
	    		spacer.setHeight(1);
	    		spacer.setWidth(LayoutParams.FILL_PARENT);
	    		spacer.setBackgroundColor(Color.parseColor("#444444"));
	    		//loc.setLayoutParams(params)
	    		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
	    		
	    		LayoutParams loclp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
	    		//loclp.setMargins(8, 16, 0, 16);	    		
	    		loc.setLayoutParams(loclp);
	    		loc.setPadding(16, 16, 0, 16);
	    		ll.setLayoutParams(lp);
	    		ll.addView(loc);
	    		ll.addView(spacer);
	    		outerlayout.addView(ll);
	    		
	    		final String finalLocationName = locations[i];
	    		
	    		ll.setOnClickListener(destroyClick(finalLocationName));
	    	}
	    }else{
    		LinearLayout ll = new LinearLayout(this);
    		ll.setOrientation(LinearLayout.VERTICAL);
    		TextView loc = new TextView(this);
    		loc.setText("No Locations Set");
    		loc.setTextSize(23);
    		
    		
    		TextView spacer = new TextView(this);
    		spacer.setHeight(1);
    		spacer.setWidth(LayoutParams.FILL_PARENT);
    		spacer.setBackgroundColor(Color.parseColor("#444444"));
    		//loc.setLayoutParams(params)
    		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    		
    		LayoutParams loclp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    		//loclp.setMargins(8, 8, 0, 8);
    		loc.setPadding(8, 8, 0, 8);
    		loc.setLayoutParams(loclp);
    		ll.setLayoutParams(lp);
    		ll.addView(loc);
    		ll.addView(spacer);
    		outerlayout.addView(ll);
    		ll.setOnClickListener(closeClick);
	    }
	    
	    
	    //ADD CLICK LISTENERS
	    
	    
	    
	    
	      //  this.setContentView(layoutResID)
	}
	
	OnClickListener closeClick = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			DeleteLocation.this.finish();
			
		}
		
	};
	
	 OnClickListener destroyClick(final String locationName){
		
		OnClickListener ret = new OnClickListener(){
		

			@Override
			public void onClick(View v) {
				try{
					
					Bundle b = new Bundle();
					b.putString("locationName", locationName);
					Intent i = new Intent(DeleteLocation.this,ConfirmDelete.class);
					i.putExtras( b);
					DeleteLocation.this.startActivity(i);
				
					DeleteLocation.this.finish();

					
					
					//SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
					//sql.execSQL("DELETE FROM LOCATIONS WHERE locationName = '"+locationName+"';");
					//sql.close();
				}catch(Exception e){
					e.printStackTrace();
				}
				//ADD MORE SHIT LATER
				DeleteLocation.this.finish();
			}
			
		};
		
		return ret;
	}
	
	
	
	
	
	private String[] getStringArSQLVertical(String query, SQLiteDatabase sql){
		String[] ret = new String[0];
		try{
			Cursor c = sql.rawQuery(query, null);
			List<String> list = new ArrayList<String>();
			if(c!=null){
				while(c.moveToNext()){
					list.add(c.getString(0));
					Log.e("TECHVENTUS","DIRECT FROM QUERY +"+c.getString(0));
				}
				if(list.size()>0){
					ret = list.toArray(new String[list.size()]);
					//for(int i=0;i<list.size();i++){
					//	ret[i] = list.get(i);
					//}
				}
				c.close();
			}
			

		}catch(Exception o){
			o.printStackTrace();
		}
		return ret;
	}
	
	
}
